<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2104 – In functions, use `return` instead of `break`.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2104 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2104">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="in-functions-use-return-instead-of-break">In functions, use
<code>return</code> instead of <code>break</code>.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2104.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">foo()</span> <span class="kw">{</span></span>
<span id="cb1-2"><a href="SC2104.html#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="cf">if</span> <span class="kw">[[</span> <span class="ot">-z</span> <span class="va">$1</span> <span class="kw">]]</span></span>
<span id="cb1-3"><a href="SC2104.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="cf">then</span></span>
<span id="cb1-4"><a href="SC2104.html#cb1-4" aria-hidden="true" tabindex="-1"></a>    <span class="cf">break</span></span>
<span id="cb1-5"><a href="SC2104.html#cb1-5" aria-hidden="true" tabindex="-1"></a>  <span class="cf">fi</span></span>
<span id="cb1-6"><a href="SC2104.html#cb1-6" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Hello </span><span class="va">$1</span><span class="st">&quot;</span></span>
<span id="cb1-7"><a href="SC2104.html#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2104.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">foo()</span> <span class="kw">{</span></span>
<span id="cb2-2"><a href="SC2104.html#cb2-2" aria-hidden="true" tabindex="-1"></a>  <span class="cf">if</span> <span class="kw">[[</span> <span class="ot">-z</span> <span class="va">$1</span> <span class="kw">]]</span></span>
<span id="cb2-3"><a href="SC2104.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="cf">then</span></span>
<span id="cb2-4"><a href="SC2104.html#cb2-4" aria-hidden="true" tabindex="-1"></a>    <span class="cf">return</span> <span class="dv">1</span></span>
<span id="cb2-5"><a href="SC2104.html#cb2-5" aria-hidden="true" tabindex="-1"></a>  <span class="cf">fi</span></span>
<span id="cb2-6"><a href="SC2104.html#cb2-6" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Hello </span><span class="va">$1</span><span class="st">&quot;</span></span>
<span id="cb2-7"><a href="SC2104.html#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>break</code> or <code>continue</code> are used to abort or
continue a loop, and are not the right way to exit a function. Use
<code>return</code> instead.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>The <code>break</code> or <code>continue</code> may be intended for a
loop that calls the function:</p>
<pre><code># Rarely valid
foo() { break; echo $?; }
while true; do foo; done</code></pre>
<p>This is undefined behavior in POSIX sh. Different shells do different
things.</p>
<p>When the function is called from a loop:</p>
<ul>
<li><code>ksh</code> keeps going and <code>$?</code> is 0.</li>
<li><code>bash</code> version 4.4+ prints an error "break: only
meaningful in a `for', `while', or `until' loop", the function keeps
going, and <code>$?</code> is 0.</li>
<li><code>bash</code> versions before 4.4, will return from the
function, break the loop calling the function, or exit a subshell if
there's one in between.</li>
<li><code>dash</code>, BusyBox <code>ash</code>: like above.</li>
</ul>
<p>When the function is not called from a loop:</p>
<ul>
<li>All <code>bash</code> versions print an error "break: only
meaningful in a `for', `while', or `until' loop", the function keeps
going, and <code>$?</code> is 0.</li>
<li><code>ksh</code>, <code>dash</code> and <code>ash</code> silently
keep going and <code>$?</code> is 0.</li>
</ul>
<p>Due to the many different implementations, many of which are not
helpful, it's recommended to use proper flow control. A typical solution
is making sure the function <code>return</code>s success/failure, and
calling <code>myfunction || break</code> in the loop.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


